package cn.ht.jywrwater.module.ht.service.villagemeter;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.ht.jywrwater.module.ht.controller.admin.villagemeter.vo.*;
import cn.ht.jywrwater.module.ht.dal.dataobject.villagemeter.VillageMeterDO;
import cn.ht.jywrwater.framework.common.pojo.PageResult;

import cn.ht.jywrwater.module.ht.convert.villagemeter.VillageMeterConvert;
import cn.ht.jywrwater.module.ht.dal.mysql.villagemeter.VillageMeterMapper;

import static cn.ht.jywrwater.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.ht.jywrwater.module.ht.enums.ErrorCodeConstants.*;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;

/**
 * 水表村庄绑定关系 Service 实现类
 *
 * @author hzx
 */
@Service
@Validated
public class VillageMeterServiceImpl implements VillageMeterService {

    @Resource
    private VillageMeterMapper villageMeterMapper;

    @Override
    public Long createVillageMeter(VillageMeterCreateReqVO createReqVO) {
        // 插入
        VillageMeterDO villageMeter = VillageMeterConvert.INSTANCE.convert(createReqVO);
        villageMeterMapper.insert(villageMeter);
        // 返回
        return villageMeter.getId();
    }

    @Override
    public void updateVillageMeter(VillageMeterUpdateReqVO updateReqVO) {
        // 校验存在
        validateVillageMeterExists(updateReqVO.getId());
        // 更新
        VillageMeterDO updateObj = VillageMeterConvert.INSTANCE.convert(updateReqVO);
        villageMeterMapper.updateById(updateObj);
    }

    @Override
    public void deleteVillageMeter(Long id) {
        // 校验存在
        validateVillageMeterExists(id);
        // 删除
        villageMeterMapper.deleteById(id);
    }

    private void validateVillageMeterExists(Long id) {
        if (villageMeterMapper.selectById(id) == null) {
            throw exception(VILLAGE_METER_NOT_EXISTS);
        }
    }

    @Override
    public VillageMeterDO getVillageMeter(Long id) {
        return villageMeterMapper.selectById(id);
    }

    @Override
    public List<VillageMeterDO> getVillageMeterList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return ListUtil.empty();
        }
        return villageMeterMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<VillageMeterDO> getVillageMeterPage(VillageMeterPageReqVO pageReqVO) {
        return villageMeterMapper.selectPage(pageReqVO);
    }

    @Override
    public List<VillageMeterDO> getVillageMeterList(VillageMeterExportReqVO exportReqVO) {
        return villageMeterMapper.selectList(exportReqVO);
    }

}
